/**
 * The contents of this file are subject to the license and copyright detailed in the LICENSE and NOTICE files at the root of the source tree and available online at
 * http://www.askosi.org/license.html
 */
package be.destin.skos.core;

import java.util.LinkedList;

import javax.xml.bind.annotation.XmlValue;

import org.openrdf.model.URI;

public class ConceptIdentifier implements URI {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static final String SESAME_NAMESPACE = "Concept:";

	String relAbout;

	public ConceptIdentifier() { // For JAXB...
		
	}

	public ConceptIdentifier (String anAbout) {
		relAbout = anAbout;
	}
	
	public String toString() {
		return this.getNamespace()+relAbout;
	}

	public String stringValue() {
		return this.getNamespace()+relAbout;
	}

	public String getNamespace() {
		return ConceptIdentifier.SESAME_NAMESPACE;
	}
	
	public String getLocalName() {
		return relAbout;
	}
	
	public boolean equals(Object o) {
		if (o == null) return false;
		if (this == o) return true;
		if (o instanceof String) {
			return this.relAbout.equals(((String)o));
		}
		if ( ! (o instanceof ConceptIdentifier)) return false;
		return this.relAbout.equals(((ConceptIdentifier)o).getAbout());
	}

	public int hashCode() {
		if (this.relAbout == null) return 0;
		return this.relAbout.hashCode();
	}

	public ConceptIdentifier clean() {
		if (relAbout == null) return null;
		String anAbout = SkosUtil.normalizeAbout(relAbout);
		if (anAbout.isEmpty()) return null;
		return new ConceptIdentifier(anAbout);
	}
	
	@XmlValue
	public String getAbout() {
		return relAbout;
	}
		
	public void setAbout(String rel) {
		this.relAbout = rel;
	}

	public Concept getConcept(SkosManager skosManager) {
		if (relAbout == null || relAbout.isEmpty()) return null;
		return skosManager.getScheme_Concept(relAbout);
	}
/* CONFLICTS WITH LinkedConcept and LocalLinkedConcept
	public static LinkedList<String> getScheme_About(LinkedList<ConceptIdentifier> rels) {
		LinkedList<String> result = new LinkedList<String>();
		if (rels == null) return result;
		for (ConceptIdentifier aRel : rels) {
			result.add(aRel.getAbout());
		}
		return result;
	}
*/
	public String toRdfObjectUrl() {
		return "&rel="+this.getAbout();
	}

	public String toJavaScriptParameters() {
		return ",rel:'"+this.getAbout();
	}
/* CONFLICTS WITH LinkedConcept and LocalLinkedConcept
	public static LinkedList<ConceptIdentifier> cleanSemanticRelation (LinkedList<ConceptIdentifier> toBeCleaned) {
		if (toBeCleaned == null) return null;
		LinkedList<ConceptIdentifier> result = new LinkedList<ConceptIdentifier>();
		for (ConceptIdentifier anEntry : toBeCleaned) {
			if (anEntry != null) {
				ConceptIdentifier cleanEntry = anEntry.clean();
				if (cleanEntry != null) {
					result.add(cleanEntry);
				}
			}
		}
		return result;
	}
*/
	public static LinkedList<ConceptIdentifier> makeIdentifiers (LinkedList<String> toBeCleaned) {
		if (toBeCleaned == null) return null;
		LinkedList<ConceptIdentifier> result = new LinkedList<ConceptIdentifier>();
		for (String anEntry : toBeCleaned) {
			if (anEntry != null) {
				String anAbout = SkosUtil.normalizeAbout(anEntry);
				if (anAbout.isEmpty()) continue;
				ConceptIdentifier cleanEntry = new ConceptIdentifier(anAbout);
				if (cleanEntry != null) {
					result.add(cleanEntry);
				}
			}
		}
		return result;
	}
}
